package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.config.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.Version;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ct_start
 * @since 2021-08-12
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    // 注入小节的service
    @Autowired
    EduVideoService eduVideoService;


    // 根据课程ID ,查询里面的章节.并且,章节里面带有所有的小节
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        // 根据课程 id 查询里面课程里面所有的章节

        QueryWrapper<EduChapter> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(queryWrapper);

        // 再根据章节查询里面所有的小节
        QueryWrapper<EduVideo> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoService.list(queryWrapper1);

        // 遍历章节进行list 封装最终的章节数据
        List<ChapterVo> list1=new ArrayList<>();
        // 遍历查出来的所有章节
        for (int i = 0; i < eduChapters.size(); i++) {

            // 创建一个 章节小节封装实体类
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapters.get(i),chapterVo);
            list1.add(chapterVo);
            // 封装所有的小节
            // 创建一个 List 用于封装所有的小节
            List <VideoVo> finalEduVideo=new ArrayList<>();
            for (int m = 0;  m< eduVideos.size(); m++) {
                EduVideo eduVideo = eduVideos.get(m); // 得到每一个小节
                // 进行比较,如果小节里面的 chapter_id .equals() 章节里面的id 则进行封装
                if (eduVideo.getChapterId().equals(eduChapters.get(i).getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    finalEduVideo.add(videoVo);
                }
            }

            chapterVo.setChildren(finalEduVideo);
        }


        return list1;

    }

    @Override
    public boolean deleteChapter(String chapterId) {
        // 如果能根据章节查询出里面的小节,我们就删除,否则直接抛出异常
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(eduVideoQueryWrapper);
        if (count>0){
            throw  new GuliException(20001,"不能删除");
        }
        else {
            int i = baseMapper.deleteById(chapterId);
            return i>0;
        }

    }


    // 根据课程ID 删除章节
    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }
}
